<?php
/**
* PHP版3DES加解密类
* 可与java的3DES(DESede)加密方式兼容
* @Author: ZhangXiLian (sontin@qq.com)
* @version: V0.2 2009.3.8
* @package TinPhp
*/
class Crypt3Des
{
    public $key    = "www.cuncun8.com"; //"www.cuncun8.com";
    public $iv    = "23456789"; //like java: private static byte[] myIV = { 50, 51, 52, 53, 54, 55, 56, 57 };
    public $is_base64    = "1"; //like java: private static byte[] myIV = { 50, 51, 52, 53, 54, 55, 56, 57 };
    /** 获取经过Md5后的key */
    public function getKey(){
        return MD5($this->key);
    }
    /** 根据key进行Des3加密
     * @param string $input 要求加密的内容 */
    public function encrypt($input)
    {
        $input = $this->padding( $input );
        $key = base64_decode($this->getKey());
        $td = mcrypt_module_open( MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');
        //使用MCRYPT_3DES算法,cbc模式
        mcrypt_generic_init($td, $key, $this->iv);
        //初始处理
        $data = mcrypt_generic($td, $input);
        //加密
        mcrypt_generic_deinit($td);
        //结束
        mcrypt_module_close($td);
        $data = $this->removeBR(base64_encode($data));
        if($this->is_base64==0){
            return $data;    //为应对特殊的空格、+对结果再次base64，zxl 2012-03-17
        }else{
            return base64_encode($data);    //为应对特殊的空格、+对结果再次base64，zxl 2012-03-17
        }
    }
    /** 根据key进行Des3解密
     * @param string $encrypted 要求解密的内容 */
    public function decrypt($encrypted)
    {
        $encrypted = base64_decode($encrypted); //为应对特殊的空格、+对结果再次base64，zxl 2012-03-17
        $encrypted = base64_decode($encrypted);
        $key = base64_decode($this->getKey());
        $td = mcrypt_module_open( MCRYPT_3DES,'',MCRYPT_MODE_CBC,'');
        //使用MCRYPT_3DES算法,cbc模式
        mcrypt_generic_init($td, $key, $this->iv);
        //初始处理
        $decrypted = mdecrypt_generic($td, $encrypted);
        //解密
        mcrypt_generic_deinit($td);
        //结束
        mcrypt_module_close($td);
        $decrypted = $this->removePadding($decrypted);
        //$decrypted = str_replace(' ','+',$decrypted);   //2012-3-16 zxl加入
        return $decrypted;
    }
    /** 填充密码，填充至8的倍数 */
    private function padding( $str ) {
        $len = 8 - strlen( $str ) % 8;
        for ( $i = 0; $i < $len; $i++ )
        {
            $str .= chr( 0 );
         }
        return $str ;
    }
    /** 删除填充符 */
    private function removePadding( $str ) {
        $len = strlen( $str );
        $newstr = "";
        $str = str_split($str);
        for ($i = 0; $i < $len; $i++ )
        {
            if ($str[$i] != chr( 0 ))
            {
                $newstr .= $str[$i];
            }
        }
        return $newstr;
    }
    /** 删除回车和换行 */
    private function removeBR( $str ) {
        $len = strlen( $str );
        $newstr = "";
        $str = str_split($str); //explode
        for ($i = 0; $i < $len; $i++ )
        {
            if ($str[$i] != '\n' and $str[$i] != '\r')
            {
                $newstr .= $str[$i];
            }
        }
        return $newstr;
    }

}

?>